package org.benjamin.lending.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.entity.BorrowInfo;
import org.benjamin.lending.domain.entity.BorrowInfo.RepaymentMethod;
import org.benjamin.lending.domain.form.AuditForm;
import org.benjamin.lending.domain.form.BorrowIndex;
import org.benjamin.lending.domain.form.ResultData;
import org.benjamin.lending.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.JstlView;

@Controller
@RequestMapping(value = "manager/borrow")
public class ManagerBorrowController {
	
	private Logger logger = LogManager.getLogger();
	
	@Autowired
	private BorrowService borrowService;
	
	@RequestMapping(value = "index")
	public ModelAndView index(@ModelAttribute BorrowIndex borrowIndex,Pageable pageable) {
		
		ModelAndView modelAndView = new ModelAndView("admin.borrow.index");
		Page<BorrowInfo> borrowInfos = this.borrowService.findBorrowPage(borrowIndex, pageable);
		modelAndView.addObject("borrowInfos",borrowInfos);
		return modelAndView;
	}
	
	@GetMapping("create")
	public ModelAndView toCreate() {
		logger.info("访问借款标创建");
		ModelAndView modelAndView = new ModelAndView("admin.borrow.create");
		modelAndView.addObject("borrowInfo",new BorrowInfo());
		modelAndView.addObject("repaymentMethods",RepaymentMethod.values());
		return modelAndView;
	}
	
	@ResponseBody
	@PostMapping("create")
	public ResultData doCreate(BorrowInfo borrowInfo) {
		ResultData data = new ResultData();
		logger.info("创建借款标");
		try {
			Object temp = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			String username = null;
			if(temp != null) {
				UserDetails userDetails = (UserDetails)temp;
				username = userDetails.getUsername();
			}
			this.borrowService.createBorrow(borrowInfo,username);
			data.setMessage("借款标创建成功");
			data.setStatus(200);
			return data;
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			data.setStatus(300);
			data.setMessage(e.getMessage());
		}
		return data;
	}
	
	@GetMapping(value = "audit/{borrowId}")
	public ModelAndView toAudit(@PathVariable("borrowId") Integer borrowId) {
		logger.info("");
		ModelAndView modelAndView = new ModelAndView("admin/borrow/audit");
		modelAndView.addObject("borrowId",borrowId);
		return modelAndView;
	}
	
	@ResponseBody
	@PostMapping(value = "audit/{borrowId}")
	public ResultData doAudit(@PathVariable("borrowId") Integer borrowId,AuditForm auditForm) {
		logger.info("借款标开始进行审核");
		auditForm.setBorrowId(borrowId);
		ResultData resultData = new ResultData();
		try{
			this.borrowService.auitBorrow(auditForm);
			resultData.setStatus(200);
			resultData.setMessage("审核成功");
			logger.info("借款标审核成功");
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			resultData.setStatus(400);
			resultData.setMessage(e.getMessage());
		}
		return resultData;
	}
	
	@ResponseBody
	@PostMapping(value = "delete/{borrowId}")
	public ResultData doDelete(@PathVariable("borrowId") Integer borrowId) {
		ResultData resultData = new ResultData();
		try {
			this.borrowService.deleteEntity(borrowId);
			resultData.setStatus(200);
			resultData.setMessage("借款标删除成功");
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
			resultData.setStatus(300);
			resultData.setMessage("借款标删除失败："+e.getMessage());
		}
		return resultData;
	}

}
